package com.qjs.activity.rookie.controller;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.qjs.activity.rookie.dto.UserInfo;
import com.qjs.activity.rookie.dto.WebMessage;
import com.qjs.course.util.WeiXinWebUtil;

@Controller
@RequestMapping("redire")
public class GetWebAction {
	private static Logger log = Logger.getLogger(GetWebAction.class);
	
	 /** 
     * 获取code参数
     */  
	@RequestMapping("one")
	public ModelAndView getCode(HttpServletRequest req, HttpServletResponse res) throws Exception{ 
		log.error("one");
		Map<String,Object> map=new HashMap<String,Object>();
	     Enumeration paramNames = req.getParameterNames();  
	    while (paramNames.hasMoreElements()) {  
	      String paramName = (String) paramNames.nextElement();   
	      String[] paramValues = req.getParameterValues(paramName);  
	      if (paramValues.length == 1) {  
	        String paramValue = paramValues[0];  
	        if (paramValue.length() != 0) {  
	          log.error("参数：" + paramName + "=" + paramValue);
	          map.put(paramName, paramValue);
	        }  
	      }
	    }
	    String code=req.getParameter("code");
	    WebMessage mess=WeiXinWebUtil.getUserMessage(code);
	    map.put("mess", mess);
	    if(mess==null){
	    	return new ModelAndView("500");
	    }else{
	    	return new ModelAndView("code",map);
	    }    
	}
	
	/** 
     * 获取USERiNFO参数
     */  
	@RequestMapping("two")
	public ModelAndView getUserInfo(HttpServletRequest req, HttpServletResponse res) throws Exception{ 
		log.error("two");
		Map<String,Object> map=new HashMap<String,Object>();
		String code=req.getParameter("code");
		log.error("code:"+code);
	    WebMessage mess=WeiXinWebUtil.getUserMessage(code);
	    map.put("mess", mess);
	    if(mess==null){
	    	return new ModelAndView("500");
	    }else{
	    	UserInfo user=WeiXinWebUtil.getUserInfo(mess.getAccess_token(), mess.getOpenid());
	    	map.put("user", user);
	    	if(user==null){	
	    		return new ModelAndView("500");
	    	}
	    	return new ModelAndView("code",map);
	    }    
	}
	
}
